package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.Repair;
import com.amsp.oa.oademo.service.RepairService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/repair")
public class RepairController {

    @Autowired
    private RepairService repairService;

    @GetMapping
    public ResponseEntity<IPage<Repair>> getRepairList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status) {
        Page<Repair> pageParam = new Page<>(page, size);
        return ResponseEntity.ok(repairService.getRepairList(pageParam, keyword, status));
    }

    @GetMapping("/my")
    public ResponseEntity<IPage<Repair>> getMyRepairs(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam String applicant) {
        Page<Repair> pageParam = new Page<>(page, size);
        return ResponseEntity.ok(repairService.getMyRepairs(pageParam, applicant));
    }

    @GetMapping("/{id}")
    public ResponseEntity<Repair> getRepair(@PathVariable Long id) {
        return ResponseEntity.ok(repairService.getById(id));
    }

    @GetMapping("/no/{repairNo}")
    public ResponseEntity<Repair> getRepairByNo(@PathVariable String repairNo) {
        return ResponseEntity.ok(repairService.getRepairByNo(repairNo));
    }

    @PostMapping("/submit")
    public ResponseEntity<Repair> submitRepair(@RequestBody Repair repair) {
        return ResponseEntity.ok(repairService.submitRepair(repair));
    }

    @PutMapping("/{id}")
    public ResponseEntity<Repair> updateRepair(
            @PathVariable Long id,
            @RequestBody Repair repair) {
        repair.setId(id);
        return ResponseEntity.ok(repairService.updateRepair(repair));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteRepair(@PathVariable Long id) {
        repairService.deleteRepair(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/{id}/handle")
    public ResponseEntity<Repair> handleRepair(
            @PathVariable Long id,
            @RequestParam String handlerName,
            @RequestParam String handleResult) {
        return ResponseEntity.ok(repairService.handleRepair(id, handlerName, handleResult));
    }

    @PostMapping("/{id}/cancel")
    public ResponseEntity<Repair> cancelRepair(@PathVariable Long id) {
        return ResponseEntity.ok(repairService.cancelRepair(id));
    }
} 